using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerpush : MonoBehaviour
{
    public float distance = 1f;
    public float distance_up = 2f;
    public LayerMask boxMask;



    GameObject box;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        Physics2D.queriesStartInColliders = false;
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, distance, boxMask);
        RaycastHit2D hit_left = Physics2D.Raycast(transform.position, Vector2.left, distance, boxMask);
        RaycastHit2D hit_up = Physics2D.Raycast(transform.position, Vector2.up, distance_up, boxMask);

        if (hit.collider != null && hit.collider.gameObject.tag == "pushable" && Input.GetKeyDown(KeyCode.Q))
        {
            box = hit.collider.gameObject;
            box.GetComponent<FixedJoint2D>().enabled = true;
            box.GetComponent<boxpull>().beingPushed = true;
            box.GetComponent<FixedJoint2D>().connectedBody = this.GetComponent<Rigidbody2D>();
        }else if (Input.GetKeyUp(KeyCode.Q))
        {
            box.GetComponent<FixedJoint2D>().enabled = false;
            box.GetComponent<boxpull>().beingPushed = false;
        }

        if (hit_left.collider != null && hit_left.collider.gameObject.tag == "pushable" && Input.GetKeyDown(KeyCode.Q))
        {
            box = hit_left.collider.gameObject;
            box.GetComponent<FixedJoint2D>().enabled = true;
            box.GetComponent<boxpull>().beingPushed = true;
            box.GetComponent<FixedJoint2D>().connectedBody = this.GetComponent<Rigidbody2D>();
        }
        else if (Input.GetKeyUp(KeyCode.Q))
        {
            box.GetComponent<FixedJoint2D>().enabled = false;
            box.GetComponent<boxpull>().beingPushed = false;
        }

        if(hit_up.collider != null && hit_up.collider.tag == "number")
        {
            hit_up.collider.GetComponent<number_box>().numberBoxBounce();
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(transform.position, (Vector2)transform.position + Vector2.right * distance);
        Gizmos.DrawLine(transform.position, (Vector2)transform.position + Vector2.left * distance);
        Gizmos.DrawLine(transform.position, (Vector2)transform.position + Vector2.up * distance_up );
    }


}
